Imports Microsoft.Xna.Framework

''' <summary>
''' A set of helpful methods for working with rectangles.
''' </summary>
Public NotInheritable Class RectangleExtensions
	Private Sub New()
	End Sub
	''' <summary>
	''' Calculates the signed depth of intersection between two rectangles.
	''' </summary>
	''' <returns>
	''' The amount of overlap between two intersecting rectangles. These
	''' depth values can be negative depending on which wides the rectangles
	''' intersect. This allows callers to determine the correct direction
	''' to push objects in order to resolve collisions.
	''' If the rectangles are not intersecting, Vector2.Zero is returned.
	''' </returns>

    Public Shared Function GetIntersectionDepth(rectA As Rectangle, rectB As Rectangle) As Vector2
        ' Calculate half sizes.
        Dim halfWidthA As Single = rectA.Width / 2.0F
        Dim halfHeightA As Single = rectA.Height / 2.0F
        Dim halfWidthB As Single = rectB.Width / 2.0F
        Dim halfHeightB As Single = rectB.Height / 2.0F

        ' Calculate centers.
        Dim centerA As New Vector2(rectA.Left + halfWidthA, rectA.Top + halfHeightA)
        Dim centerB As New Vector2(rectB.Left + halfWidthB, rectB.Top + halfHeightB)

        ' Calculate current and minimum-non-intersecting distances between centers.
        Dim distanceX As Single = centerA.X - centerB.X
        Dim distanceY As Single = centerA.Y - centerB.Y
        Dim minDistanceX As Single = halfWidthA + halfWidthB
        Dim minDistanceY As Single = halfHeightA + halfHeightB

        ' If we are not intersecting at all, return (0, 0).
        If Math.Abs(distanceX) >= minDistanceX OrElse Math.Abs(distanceY) >= minDistanceY Then
            Return Vector2.Zero
        End If

        ' Calculate and return intersection depths.
        Dim depthX As Single = If(distanceX > 0, minDistanceX - distanceX, -minDistanceX - distanceX)
        Dim depthY As Single = If(distanceY > 0, minDistanceY - distanceY, -minDistanceY - distanceY)
        Return New Vector2(depthX, depthY)
    End Function

	''' <summary>
	''' Gets the position of the center of the bottom edge of the rectangle.
	''' </summary>

    Public Shared Function GetBottomCenter(rect As Rectangle) As Vector2
        Return New Vector2(rect.X + rect.Width / 2.0F, rect.Bottom)
    End Function
End Class
